package com.app.bimo.mvp.ui.activity;

import android.widget.Button;
import android.widget.FrameLayout;

import com.app.bimo.R;
import com.app.bimo.commonbusiness.base.BaseActivity;
import com.app.bimo.commonbusiness.util.Constant;
import com.app.bimo.commonbusiness.util.JumpPageUtil;
import com.app.bimo.commonbusiness.util.RouterHub;
import com.app.bimo.commonbusiness.util.SharedPreUtils;
import com.app.bimo.commonbusiness.util.ToastUtils;
import com.app.bimo.library_db.UserData;
import com.app.bimo.library_db.helper.UserHelper;
import com.app.bimo.module_accounts.mvp.ui.activity.PreferenceActivity;
import com.app.bimo.module_bookrack.mvp.ui.fragment.BookRackFragment;
import com.app.bimo.module_bookrack.mvp.ui.fragment.FindFragment;
import com.app.bimo.module_bookrack.mvp.ui.fragment.MineFragment;
import com.app.bimo.mvp.contract.MainContract;
import com.app.bimo.mvp.presenter.MainPresenter;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    private Button test;

    @BindView(R.id.bottom_navigation)
    private BottomNavigationView bottomBar;
    @BindView(R.id.container)
    private FrameLayout container;


    //跳转相关
    private FragmentManager mfragmentManager;
    private Fragment from;
    private Fragment to;

    private BookRackFragment bookRackFragment;
    private MineFragment mineFragment;
    private FindFragment findFragment;


    @Override
    protected void initView() {
//        test = findViewById(R.id.test);

        bottomBar = findViewById(R.id.bottom_navigation);
        container = findViewById(R.id.container);
        bottomBar.setItemIconTintList(null);
        mfragmentManager = getSupportFragmentManager();
    }

    @Override
    protected void initData() {

        bookRackFragment = new BookRackFragment();
        mineFragment = new MineFragment();
        findFragment = new FindFragment();
        switchContent(from, bookRackFragment);
    }

    @Override
    protected void initEvent() {
        JumpPageUtil.jumpActivity(MainActivity.this, PreferenceActivity.class,false);
        if (SharedPreUtils.getInstance(getApplicationContext()).getBoolean(Constant.isDevicesLoginSucess, false)) {
            settingUserPre();

        }


        bottomBar.setOnNavigationItemSelectedListener(menuItem -> {
            String path = "";
            switch (menuItem.getItemId()) {
                case R.id.bookselfFragment:
                    ToastUtils.showToast(MainActivity.this,"11");
                    path = RouterHub.BOOKRACK_MAIN;
                    switchContent(from, bookRackFragment);
                    break;
                case R.id.bookStorFragment:
                    ToastUtils.showToast(MainActivity.this,"22");
                    path = RouterHub.HOME_MAIN;
                    switchContent(from, findFragment);
                    break;
                case R.id.findFragment:
                    ToastUtils.showToast(MainActivity.this,"33");
                    path = RouterHub.BOOKRACK_FIND;
                    switchContent(from, mineFragment);
                    break;
                case R.id.mineFragment:
                    ToastUtils.showToast(MainActivity.this,"44");
                    path = RouterHub.USER_MINE;
                    switchContent(from, findFragment);
                    break;
            }

            return true;
        });



    }


    /**
     * 设置用户偏好
     * 一个设备 只触发一次
     * 并且在
     * 幽灵账户注册时候  页面在书架 书城的时候
     * (controller.getCurrentDestination().getLabel().equals(RouterHub.BOOKRACK_MAIN)
     * || controller.getCurrentDestination().getLabel().equals(RouterHub.HOME_MAIN)))
     */
    private void settingUserPre() {
        //不允许返回？
        boolean settingPreNotAllowBack = SharedPreUtils.getInstance(getApplicationContext()).getBoolean(Constant.settingPreNotAllowBack, false);
        //是否设置成功过偏好设置
        boolean settingPreSucess = SharedPreUtils.getInstance(getApplicationContext()).getBoolean(Constant.settingPreSucess, false);
        UserData data = UserHelper.getsInstance().findUser();
        if (data == null) {
            return;
        }
        if (!settingPreNotAllowBack && !settingPreSucess && data.getChannel() == 0) {
            //是幽灵账户 并且 没有设置过偏好   channel == 0
            if (data.getType() == 1 && data.getChannel() == 0) {
//                SharedPreUtils.getInstance(getApplicationContext()).putString(Constant.isFirstSettingPreferences, Constant.isFirstSettingPreferences);
//                bundle.putBoolean(Constant.PreferenceHaveTop, false);
//                bundle.putInt(Constant.isRegister, 1);
//                ARUtil.navigationFragment(RouterHub.LOGIN_PREFERENCE, view, bundle);
                //跳转到偏好设置
                JumpPageUtil.jumpActivity(MainActivity.this, PreferenceActivity.class,false);
            }
        }

    }



    @Override
    protected int setLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void onResume() {
        super.onResume();
//        test.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showEmpty();
//            }
//        });
//        Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_LONG).show();
    }


    public void switchContent(Fragment from, Fragment to) {

        FragmentTransaction transaction = mfragmentManager.beginTransaction();
        if (from == null) {
            transaction.add(R.id.container, to).commit();
            this.from = to;
            return;
        }
        if (from != to) {
            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.hide(from).add(R.id.container, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
            this.from = to;
        }
    }


}
